﻿using FluentAssertions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using NewRetail.Application.Components.Warehouse;
using NewRetail.Application.Components.Warehouse.Entity;
using NewRetail.Application.Core;
using System;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;

namespace NewRetail.Api.Controllers
{
    [NewRetailRoute("wholesale-receipt")]
    public class WholeSaleReceiptController : BaseController<WholesaleReceipt>
    {
        private readonly IWarehouseGoodsService warehouseGoodsService;

        public WholeSaleReceiptController(IWarehouseGoodsService warehouseGoodsService)
        {
            this.warehouseGoodsService = warehouseGoodsService;
        }

        [HttpPost("save-multiple")]
        public async Task<ResponseEntity<WholesaleReceipt[]>> SaveMultiple([FromBody] ReceiptNoItemsDto<WholesaleReceipt> data)
        {
            var res = await this.Repo.SaveReceiptItems(this.HttpContext.RequestServices, data, "WR");
            return new ResponseEntity<WholesaleReceipt[]>().SetData(res.Items);
        }

        [HttpPost("query-by-receipt-no")]
        public async Task<ResponseEntity<WholesaleReceipt[]>> QueryByReceiptNo([FromBody] ReceiptNoDto dto)
        {
            dto.Should().NotBeNull();
            dto.receiptNo.Should().NotBeNullOrEmpty();
            var query = this.SingleStoreService.ApplyToQueryable(this.Repo);
            var data = await query.Where(x => x.receiptNo == dto.receiptNo).ToArrayAsync();

            return new ResponseEntity<WholesaleReceipt[]>().SetData(data);
        }
    }
}
